<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="120px" :inline="true">
            <el-form-item label="平台商品分类：">
              <el-cascader
                v-model="tableFrom.cate_id"
                class="selWidth "
                :options="brandCategory"
                clearable
                :props="props"
                @change="getList(1)"
              />
            </el-form-item>
            <el-form-item label="关键字检索：" class="width100" style="display: inline-block;">
              <el-input
                v-model="tableFrom.keyword"
                @keyup.enter.native="getList(1)"
                placeholder="请输入商品名称,关键字"
                class="selWidth"
                size="small"
              >
                <el-button slot="append" icon="el-icon-search" size="small" class="el-button-solt" @click="getList(1),getCardList()" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to="{ path: roterPre+ '/product/addProductPlatfrom' }">
          <el-button size="small" type="primary">添加商品</el-button>
        </router-link>
        <el-button size="small" type="success" @click="onCopy">商品采集</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        row-key="product_id"
        :default-expand-all="false"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="product_id" label="ID" min-width="60" />
        <el-table-column label="商品图" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image style="width: 36px; height: 36px" :src="scope.row.image" :preview-src-list="[scope.row.image]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="store_name" min-width="150" />
        <el-table-column label="商品售价" prop="price" min-width="150" />
        <el-table-column prop="sort" label="排序" min-width="50" />
        <el-table-column prop="create_time" label="创建时间" min-width="150" />
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{ path: roterPre+ '/product/addProductPlatfrom/'+scope.row.product_id }">
              <el-button size="small" type="text">编辑</el-button>
            </router-link>

            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.product_id, scope.$index)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!-- 生成淘宝京东表单-->
    <tao-bao ref="taoBao" @getSuccess="getSuccess" />
    </div>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  categoryListApi,
  platformproductLstApi,
  brandCreateApi,
  brandUpdateApi,
  productDeleteApi,
} from "@/api/product";
import { roterPre } from "@/settings";
import taoBao from "./taoBao";
export default {
  name: "BrandList",
  components: { taoBao },
  data() {
    return {
      roterPre: roterPre,
      props: {
        value: "value",
        label: "label",
        children: "children",
        emitPath: false,
      },
      isChecked: false,
      listLoading: true,
      tableData: {
        data: [],
        total: 0,
      },
      lstFilterForm: {
        is_platform: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        mer_cate_id: '',
        cate_id: '',
        keyword: '',
        type: '1',
        is_gift_bag: '',
        us_status: '',
        mer_labels: '',
        product_id: this.$route.query.id ? this.$route.query.id : ""
      },
      imgList: [],
      brandCategory: [],
    };
  },
  mounted() {
    this.getCategory();
    this.getList();
  },
  methods: {
    // 品牌分类
    getCategory() {
      categoryListApi({ page: 1, limit: 9999,status: 1 })
        .then((res) => {
          this.brandCategory = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      platformproductLstApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.tableData.data.map((item) => {
            this.imgList.push(item.pic);
          });
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 添加
    onAdd() {
      this.$modalForm(brandCreateApi()).then(() => this.getList());
    },
    // 编辑
    onEdit(id) {
      this.$modalForm(brandUpdateApi(id)).then(() => this.getList());
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure('删除该商品').then(() => {
        productDeleteApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.getList();
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    // 复制淘宝
    onCopy() {
      this.$refs.taoBao.modals = true;
      this.$refs.taoBao.soure_link = "";
      this.$refs.taoBao.formValidate = {};
      this.$refs.taoBao.isData = false;
    },
    getSuccess() {
      this.getList();
    },
  },
};
</script>

<style scoped lang="scss">
.selWidth {
  width: 300px !important;
}
</style>
